#define MS_CLASS "RoomInfo"

#include "RoomInfo/RoomInfo.h"
#include "HttpServer.h"
#include "Channel/Request.hpp"
#include "DepLibUV.hpp"
#include "Logger.hpp"
#include "MediaSoupErrors.hpp"
#include "Settings.hpp"
#include "iostream"

// vp8
// RoomInfo::RoomInfo() 
// {
// 	routerRtpCapabilities = json::parse(R"({"codecs":[{"kind":"audio","mimeType":"audio/opus","clockRate":48000,"channels":2,"rtcpFeedback":[{"type":"transport-cc","parameter":""}],"parameters":{},"preferredPayloadType":100},{"kind":"video","mimeType":"video/VP8","clockRate":90000,"rtcpFeedback":[{"type":"nack","parameter":""},{"type":"nack","parameter":"pli"},{"type":"ccm","parameter":"fir"},{"type":"goog-remb","parameter":""},{"type":"transport-cc","parameter":""}],"parameters":{},"preferredPayloadType":101},{"kind":"video","mimeType":"video/rtx","preferredPayloadType":102,"clockRate":90000,"parameters":{"apt":101},"rtcpFeedback":[]},{"kind":"video","mimeType":"video/H264","clockRate":90000,"parameters":{"packetization-mode":1,"level-asymmetry-allowed":1,"profile-level-id":"4d0032"},"rtcpFeedback":[{"type":"nack","parameter":""},{"type":"nack","parameter":"pli"},{"type":"ccm","parameter":"fir"},{"type":"goog-remb","parameter":""},{"type":"transport-cc","parameter":""}],"preferredPayloadType":103},{"kind":"video","mimeType":"video/rtx","preferredPayloadType":104,"clockRate":90000,"parameters":{"apt":103},"rtcpFeedback":[]},{"kind":"video","mimeType":"video/H264","clockRate":90000,"parameters":{"packetization-mode":1,"level-asymmetry-allowed":1,"profile-level-id":"42e01f"},"rtcpFeedback":[{"type":"nack","parameter":""},{"type":"nack","parameter":"pli"},{"type":"ccm","parameter":"fir"},{"type":"goog-remb","parameter":""},{"type":"transport-cc","parameter":""}],"preferredPayloadType":105},{"kind":"video","mimeType":"video/rtx","preferredPayloadType":106,"clockRate":90000,"parameters":{"apt":105},"rtcpFeedback":[]}],"headerExtensions":[{"kind":"audio","uri":"urn:ietf:params:rtp-hdrext:sdes:mid","preferredId":1,"preferredEncrypt":false,"direction":"sendrecv"},{"kind":"video","uri":"urn:ietf:params:rtp-hdrext:sdes:mid","preferredId":1,"preferredEncrypt":false,"direction":"sendrecv"},{"kind":"video","uri":"urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id","preferredId":2,"preferredEncrypt":false,"direction":"recvonly"},{"kind":"video","uri":"urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id","preferredId":3,"preferredEncrypt":false,"direction":"recvonly"},{"kind":"audio","uri":"http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time","preferredId":4,"preferredEncrypt":false,"direction":"sendrecv"},{"kind":"video","uri":"http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time","preferredId":4,"preferredEncrypt":false,"direction":"sendrecv"},{"kind":"audio","uri":"http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01","preferredId":5,"preferredEncrypt":false,"direction":"recvonly"},{"kind":"video","uri":"http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01","preferredId":5,"preferredEncrypt":false,"direction":"sendrecv"},{"kind":"video","uri":"http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07","preferredId":6,"preferredEncrypt":false,"direction":"sendrecv"},{"kind":"video","uri":"urn:ietf:params:rtp-hdrext:framemarking","preferredId":7,"preferredEncrypt":false,"direction":"sendrecv"},{"kind":"audio","uri":"urn:ietf:params:rtp-hdrext:ssrc-audio-level","preferredId":10,"preferredEncrypt":false,"direction":"sendrecv"},{"kind":"video","uri":"urn:3gpp:video-orientation","preferredId":11,"preferredEncrypt":false,"direction":"sendrecv"},{"kind":"video","uri":"urn:ietf:params:rtp-hdrext:toffset","preferredId":12,"preferredEncrypt":false,"direction":"sendrecv"}]})");
// 	mediaCodecs = json::parse(R"([{"kind":"audio","mimeType":"audio/opus","clockRate":48000,"channels":2},{"kind":"video","mimeType":"video/VP8","clockRate":90000,"parameters":{}},{"kind":"video","mimeType":"video/h264","clockRate":90000,"parameters":{"packetization-mode":1,"profile-level-id":"4d0032","level-asymmetry-allowed":1}},{"kind":"video","mimeType":"video/h264","clockRate":90000,"parameters":{"packetization-mode":1,"profile-level-id":"42e01f","level-asymmetry-allowed":1}}])");
// 	rtpMapping = json::parse(R"({"codecs":[{"payloadType":96,"mappedPayloadType":101},{"payloadType":97,"mappedPayloadType":102}],"encodings":[{"mappedSsrc":729521403,"rid":"r0","scalabilityMode":"S1T3"},{"mappedSsrc":729521404,"rid":"r1","scalabilityMode":"S1T3"}]})");
// 	consumableRtpParameters = json::parse(R"({"codecs":[{"mimeType":"video/VP8","payloadType":101,"clockRate":90000,"parameters":{},"rtcpFeedback":[{"type":"nack","parameter":""},{"type":"nack","parameter":"pli"},{"type":"ccm","parameter":"fir"},{"type":"goog-remb","parameter":""},{"type":"transport-cc","parameter":""}]},{"mimeType":"video/rtx","payloadType":102,"clockRate":90000,"parameters":{"apt":101},"rtcpFeedback":[]}],"headerExtensions":[{"uri":"urn:ietf:params:rtp-hdrext:sdes:mid","id":1,"encrypt":false,"parameters":{}},{"uri":"http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time","id":4,"encrypt":false,"parameters":{}},{"uri":"http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01","id":5,"encrypt":false,"parameters":{}},{"uri":"http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07","id":6,"encrypt":false,"parameters":{}},{"uri":"urn:ietf:params:rtp-hdrext:framemarking","id":7,"encrypt":false,"parameters":{}},{"uri":"urn:3gpp:video-orientation","id":11,"encrypt":false,"parameters":{}},{"uri":"urn:ietf:params:rtp-hdrext:toffset","id":12,"encrypt":false,"parameters":{}}],"encodings":[{"active":true,"scaleResolutionDownBy":4,"maxBitrate":96000,"scalabilityMode":"S1T3","dtx":false,"ssrc":729521403},{"active":true,"scaleResolutionDownBy":1,"maxBitrate":680000,"scalabilityMode":"S1T3","dtx":false,"ssrc":729521404}],"rtcp":{"cname":"ab05199b","reducedSize":true,"mux":true}})");
// 	audiortpMapping = json::parse(R"({"codecs":[{"payloadType":111,"mappedPayloadType":100}],"encodings":[{"mappedSsrc":618504650,"ssrc":1971670989}]})");
// 	audioconsumableRtpParameters = json::parse(R"( {"codecs":[{"mimeType":"audio/opus","payloadType":100,"clockRate":48000,"channels":2,"parameters":{"minptime":10,"useinbandfec":1},"rtcpFeedback":[{"type":"transport-cc","parameter":""}]}],"headerExtensions":[{"uri":"urn:ietf:params:rtp-hdrext:sdes:mid","id":1,"encrypt":false,"parameters":{}},{"uri":"http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time","id":4,"encrypt":false,"parameters":{}},{"uri":"urn:ietf:params:rtp-hdrext:ssrc-audio-level","id":10,"encrypt":false,"parameters":{}}],"encodings":[{"ssrc":618504650,"dtx":false}],"rtcp":{"cname":"0Cn91iZVKluwWvgG","reducedSize":true,"mux":true}})");
// 	rtpParameters = json::parse(R"({"codecs":[{"mimeType":"video/VP8","payloadType":101,"clockRate":90000,"parameters":{},"rtcpFeedback":[{"type":"transport-cc","parameter":""},{"type":"ccm","parameter":"fir"},{"type":"nack","parameter":""},{"type":"nack","parameter":"pli"}]},{"mimeType":"video/rtx","payloadType":102,"clockRate":90000,"parameters":{"apt":101},"rtcpFeedback":[]}],"headerExtensions":[{"uri":"urn:ietf:params:rtp-hdrext:sdes:mid","id":1,"encrypt":false,"parameters":{}},{"uri":"http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time","id":4,"encrypt":false,"parameters":{}},{"uri":"http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01","id":5,"encrypt":false,"parameters":{}},{"uri":"urn:3gpp:video-orientation","id":11,"encrypt":false,"parameters":{}},{"uri":"urn:ietf:params:rtp-hdrext:toffset","id":12,"encrypt":false,"parameters":{}}],"encodings":[{"ssrc":704824872,"rtx":{"ssrc":806392479},"scalabilityMode":"S2T3","maxBitrate":680000}],"rtcp":{"cname":"ab05199b","reducedSize":true,"mux":true}})");
// 	createRouter();
// 	createAudioObserver();
// }
//h264
RoomInfo::RoomInfo() {
    routerRtpCapabilities = json::parse(
            R"({"codecs":[{"kind":"audio","mimeType":"audio/opus","clockRate":48000,"channels":2,"rtcpFeedback":[{"type":"transport-cc","parameter":""}],"parameters":{},"preferredPayloadType":100},{"kind":"video","mimeType":"video/H264","clockRate":90000,"parameters":{"packetization-mode":1,"level-asymmetry-allowed":1,"profile-level-id":"4d0032"},"rtcpFeedback":[{"type":"nack","parameter":""},{"type":"nack","parameter":"pli"},{"type":"ccm","parameter":"fir"},{"type":"goog-remb","parameter":""},{"type":"transport-cc","parameter":""}],"preferredPayloadType":101},{"kind":"video","mimeType":"video/rtx","preferredPayloadType":102,"clockRate":90000,"parameters":{"apt":101},"rtcpFeedback":[]},{"kind":"video","mimeType":"video/H264","clockRate":90000,"parameters":{"packetization-mode":1,"level-asymmetry-allowed":1,"profile-level-id":"42e01f"},"rtcpFeedback":[{"type":"nack","parameter":""},{"type":"nack","parameter":"pli"},{"type":"ccm","parameter":"fir"},{"type":"goog-remb","parameter":""},{"type":"transport-cc","parameter":""}],"preferredPayloadType":103},{"kind":"video","mimeType":"video/rtx","preferredPayloadType":104,"clockRate":90000,"parameters":{"apt":103},"rtcpFeedback":[]}],"headerExtensions":[{"kind":"audio","uri":"urn:ietf:params:rtp-hdrext:sdes:mid","preferredId":1,"preferredEncrypt":false,"direction":"sendrecv"},{"kind":"video","uri":"urn:ietf:params:rtp-hdrext:sdes:mid","preferredId":1,"preferredEncrypt":false,"direction":"sendrecv"},{"kind":"video","uri":"urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id","preferredId":2,"preferredEncrypt":false,"direction":"recvonly"},{"kind":"video","uri":"urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id","preferredId":3,"preferredEncrypt":false,"direction":"recvonly"},{"kind":"audio","uri":"http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time","preferredId":4,"preferredEncrypt":false,"direction":"sendrecv"},{"kind":"video","uri":"http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time","preferredId":4,"preferredEncrypt":false,"direction":"sendrecv"},{"kind":"audio","uri":"http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01","preferredId":5,"preferredEncrypt":false,"direction":"recvonly"},{"kind":"video","uri":"http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01","preferredId":5,"preferredEncrypt":false,"direction":"sendrecv"},{"kind":"video","uri":"http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07","preferredId":6,"preferredEncrypt":false,"direction":"sendrecv"},{"kind":"video","uri":"urn:ietf:params:rtp-hdrext:framemarking","preferredId":7,"preferredEncrypt":false,"direction":"sendrecv"},{"kind":"audio","uri":"urn:ietf:params:rtp-hdrext:ssrc-audio-level","preferredId":10,"preferredEncrypt":false,"direction":"sendrecv"},{"kind":"video","uri":"urn:3gpp:video-orientation","preferredId":11,"preferredEncrypt":false,"direction":"sendrecv"},{"kind":"video","uri":"urn:ietf:params:rtp-hdrext:toffset","preferredId":12,"preferredEncrypt":false,"direction":"sendrecv"}]})");
    mediaCodecs = json::parse(
            R"([{"kind":"audio","mimeType":"audio/opus","clockRate":48000,"channels":2},{"kind":"video","mimeType":"video/h264","clockRate":90000,"parameters":{"packetization-mode":1,"profile-level-id":"4d0032","level-asymmetry-allowed":1}},{"kind":"video","mimeType":"video/h264","clockRate":90000,"parameters":{"packetization-mode":1,"profile-level-id":"42e01f","level-asymmetry-allowed":1}}])");
    rtpMapping = json::parse(
            R"({"codecs":[{"payloadType":124,"mappedPayloadType":101},{"payloadType":119,"mappedPayloadType":102}],"encodings":[{"mappedSsrc":871801942,"rid":"r0","scalabilityMode":"S1T3"}]})");
    consumableRtpParameters = json::parse(
            R"({"codecs":[{"mimeType":"video/H264","payloadType":103,"clockRate":90000,"parameters":{"level-asymmetry-allowed":1,"packetization-mode":1,"profile-level-id":"42e01f"},"rtcpFeedback":[{"type":"nack","parameter":""},{"type":"nack","parameter":"pli"},{"type":"ccm","parameter":"fir"},{"type":"goog-remb","parameter":""},{"type":"transport-cc","parameter":""}]},{"mimeType":"video/rtx","payloadType":104,"clockRate":90000,"parameters":{"apt":103},"rtcpFeedback":[]}],"headerExtensions":[{"uri":"urn:ietf:params:rtp-hdrext:sdes:mid","id":1,"encrypt":false,"parameters":{}},{"uri":"http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time","id":4,"encrypt":false,"parameters":{}},{"uri":"http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01","id":5,"encrypt":false,"parameters":{}},{"uri":"http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07","id":6,"encrypt":false,"parameters":{}},{"uri":"urn:ietf:params:rtp-hdrext:framemarking","id":7,"encrypt":false,"parameters":{}},{"uri":"urn:3gpp:video-orientation","id":11,"encrypt":false,"parameters":{}},{"uri":"urn:ietf:params:rtp-hdrext:toffset","id":12,"encrypt":false,"parameters":{}}],"encodings":[{"active":true,"scaleResolutionDownBy":1,"maxBitrate":680000,"scalabilityMode":"S1T3","dtx":false,"ssrc":871801943}],"rtcp":{"cname":"ab05199b","reducedSize":true,"mux":true}})");
            //R"({"codecs":[{"mimeType":"video/H264","payloadType":103,"clockRate":90000,"parameters":{"level-asymmetry-allowed":1,"packetization-mode":1,"profile-level-id":"42e01f"},"rtcpFeedback":[{"type":"nack","parameter":""},{"type":"nack","parameter":"pli"},{"type":"ccm","parameter":"fir"},{"type":"goog-remb","parameter":""},{"type":"transport-cc","parameter":""}]},{"mimeType":"video/rtx","payloadType":104,"clockRate":90000,"parameters":{"apt":103},"rtcpFeedback":[]}],"headerExtensions":[{"uri":"urn:ietf:params:rtp-hdrext:sdes:mid","id":1,"encrypt":false,"parameters":{}},{"uri":"http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time","id":4,"encrypt":false,"parameters":{}},{"uri":"http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01","id":5,"encrypt":false,"parameters":{}},{"uri":"http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07","id":6,"encrypt":false,"parameters":{}},{"uri":"urn:ietf:params:rtp-hdrext:framemarking","id":7,"encrypt":false,"parameters":{}},{"uri":"urn:3gpp:video-orientation","id":11,"encrypt":false,"parameters":{}},{"uri":"urn:ietf:params:rtp-hdrext:toffset","id":12,"encrypt":false,"parameters":{}}],"encodings":[{"active":true,"scaleResolutionDownBy":0,"scalabilityMode":"S1T3","dtx":false,"ssrc":871801943}],"rtcp":{"cname":"ab05199b","reducedSize":true,"mux":true}})");

    audiortpMapping = json::parse(
            R"({"codecs":[{"payloadType":111,"mappedPayloadType":100}],"encodings":[{"mappedSsrc":618504650,"ssrc":1971670989}]})");
    audioconsumableRtpParameters = json::parse(
            R"( {"codecs":[{"mimeType":"audio/opus","payloadType":100,"clockRate":48000,"channels":2,"parameters":{"minptime":10,"useinbandfec":1},"rtcpFeedback":[{"type":"transport-cc","parameter":""}]}],"headerExtensions":[{"uri":"urn:ietf:params:rtp-hdrext:sdes:mid","id":1,"encrypt":false,"parameters":{}},{"uri":"http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time","id":4,"encrypt":false,"parameters":{}},{"uri":"urn:ietf:params:rtp-hdrext:ssrc-audio-level","id":10,"encrypt":false,"parameters":{}}],"encodings":[{"ssrc":618504650,"dtx":false}],"rtcp":{"cname":"0Cn91iZVKluwWvgG","reducedSize":true,"mux":true}})");

    rtpParameters = json::parse(
            R"({"codecs":[{"mimeType":"video/H264","payloadType":101,"clockRate":90000,"parameters":{"level-asymmetry-allowed":1,"packetization-mode":1,"profile-level-id":"4d001f"},"rtcpFeedback":[{"type":"transport-cc","parameter":""},{"type":"ccm","parameter":"fir"},{"type":"nack","parameter":""},{"type":"nack","parameter":"pli"}]},{"mimeType":"video/rtx","payloadType":102,"clockRate":90000,"parameters":{"apt":101},"rtcpFeedback":[]}],"headerExtensions":[{"uri":"urn:ietf:params:rtp-hdrext:sdes:mid","id":1,"encrypt":false,"parameters":{}},{"uri":"http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time","id":4,"encrypt":false,"parameters":{}},{"uri":"http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01","id":5,"encrypt":false,"parameters":{}},{"uri":"urn:3gpp:video-orientation","id":11,"encrypt":false,"parameters":{}},{"uri":"urn:ietf:params:rtp-hdrext:toffset","id":12,"encrypt":false,"parameters":{}}],"encodings":[{"ssrc":452081599,"rtx":{"ssrc":152273997},"scalabilityMode":"S2T3","maxBitrate":680000}],"rtcp":{"cname":"ab05199b","reducedSize":true,"mux":true}})");
    //        R"({"codecs":[{"mimeType":"video/H264","payloadType":101,"clockRate":90000,"parameters":{"level-asymmetry-allowed":1,"packetization-mode":1,"profile-level-id":"4d001f"},"rtcpFeedback":[{"type":"transport-cc","parameter":""},{"type":"ccm","parameter":"fir"},{"type":"nack","parameter":""},{"type":"nack","parameter":"pli"}]},{"mimeType":"video/rtx","payloadType":102,"clockRate":90000,"parameters":{"apt":101},"rtcpFeedback":[]}],"headerExtensions":[{"uri":"urn:ietf:params:rtp-hdrext:sdes:mid","id":1,"encrypt":false,"parameters":{}},{"uri":"http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time","id":4,"encrypt":false,"parameters":{}},{"uri":"http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01","id":5,"encrypt":false,"parameters":{}},{"uri":"urn:3gpp:video-orientation","id":11,"encrypt":false,"parameters":{}},{"uri":"urn:ietf:params:rtp-hdrext:toffset","id":12,"encrypt":false,"parameters":{}}],"encodings":[{"ssrc":452081599,"rtx":{"ssrc":152273997},"scalabilityMode":"S2T3"}],"rtcp":{"cname":"ab05199b","reducedSize":true,"mux":true}})");

    createRouter();
    createAudioObserver();
}

void RoomInfo::createRouter() {
    json req_json;
    req_json["id"] = HttpServer::message_id++;
    req_json["internal"]["routerId"] = routerId;
    req_json["method"] = "worker.createRouter";
    Channel::Request req(req_json);
    OnChannelRequest(&req);
}

void RoomInfo::createAudioObserver() {
    json req_json;
    req_json["id"] = HttpServer::message_id++;
    req_json["internal"]["routerId"] = routerId;
    req_json["internal"]["rtpObserverId"] = audioObserverId;
    req_json["method"] = "router.createAudioLevelObserver";
    req_json["data"] = json::parse(R"({"maxEntries":1,"threshold":-80,"interval":800})");
    Channel::Request req(req_json);
    OnChannelRequest(&req);
}

void RoomInfo::OnChannelRequest(Channel::Request *request) {
    MS_TRACE();

            MS_DEBUG_DEV(
            "Channel request received [method:%s, id:%" PRIu32 "]", request->method.c_str(), request->id);

    switch (request->methodId) {
        case Channel::Request::MethodId::WORKER_DUMP: {
            json data = json::object();

            FillJson(data);

            request->Accept(data);

            break;
        }

        case Channel::Request::MethodId::WORKER_GET_RESOURCE_USAGE: {
            json data = json::object();

            FillJsonResourceUsage(data);

            request->Accept(data);

            break;
        }

        case Channel::Request::MethodId::WORKER_UPDATE_SETTINGS: {
            Settings::HandleRequest(request);

            break;
        }

        case Channel::Request::MethodId::WORKER_CREATE_ROUTER: {


            router = new RTC::Router(routerId);

                    MS_DEBUG_DEV("Router created [routerId:%s]", routerId.c_str());

            request->Accept();

            break;
        }

        case Channel::Request::MethodId::ROUTER_CLOSE: {

            delete router;

                    MS_DEBUG_DEV("Router closed [id:%s]", router->id.c_str());

            request->Accept();

            break;
        }

            // Any other request must be delivered to the corresponding Router.
        default: {

            router->HandleRequest(request);

            break;
        }
    }
}

void RoomInfo::FillJson(json &jsonObject) const {
    MS_TRACE();

    // Add pid.
    jsonObject["pid"] = Logger::pid;

    // Add routerIds.
    jsonObject["routerIds"] = json::array();
    auto jsonRouterIdsIt = jsonObject.find("routerIds");

    jsonRouterIdsIt->emplace_back(routerId);

}

void RoomInfo::FillJsonResourceUsage(json &jsonObject) const {
    MS_TRACE();

    int err;
    uv_rusage_t uvRusage; // NOLINT(cppcoreguidelines-pro-type-member-init)

    err = uv_getrusage(std::addressof(uvRusage));

    if (err != 0)
        MS_THROW_ERROR("uv_getrusagerequest() failed: %s", uv_strerror(err));

    // Add ru_utime (uv_timeval_t, user CPU time used, converted to ms).
    jsonObject["ru_utime"] =
            (uvRusage.ru_utime.tv_sec * static_cast<uint64_t>(1000)) + (uvRusage.ru_utime.tv_usec / 1000);

    // Add ru_stime (uv_timeval_t, system CPU time used, converted to ms).
    jsonObject["ru_stime"] =
            (uvRusage.ru_stime.tv_sec * static_cast<uint64_t>(1000)) + (uvRusage.ru_stime.tv_usec / 1000);

    // Add ru_maxrss (uint64_t, maximum resident set size).
    jsonObject["ru_maxrss"] = uvRusage.ru_maxrss;

    // Add ru_ixrss (uint64_t, integral shared memory size).
    jsonObject["ru_ixrss"] = uvRusage.ru_ixrss;

    // Add ru_idrss (uint64_t, integral unshared data size).
    jsonObject["ru_idrss"] = uvRusage.ru_idrss;

    // Add ru_isrss (uint64_t, integral unshared stack size).
    jsonObject["ru_isrss"] = uvRusage.ru_isrss;

    // Add ru_minflt (uint64_t, page reclaims, soft page faults).
    jsonObject["ru_minflt"] = uvRusage.ru_minflt;

    // Add ru_majflt (uint64_t, page faults, hard page faults).
    jsonObject["ru_majflt"] = uvRusage.ru_majflt;

    // Add ru_nswap (uint64_t, swaps).
    jsonObject["ru_nswap"] = uvRusage.ru_nswap;

    // Add ru_inblock (uint64_t, block input operations).
    jsonObject["ru_inblock"] = uvRusage.ru_inblock;

    // Add ru_oublock (uint64_t, block output operations).
    jsonObject["ru_oublock"] = uvRusage.ru_oublock;

    // Add ru_msgsnd (uint64_t, IPC messages sent).
    jsonObject["ru_msgsnd"] = uvRusage.ru_msgsnd;

    // Add ru_msgrcv (uint64_t, IPC messages received).
    jsonObject["ru_msgrcv"] = uvRusage.ru_msgrcv;

    // Add ru_nsignals (uint64_t, signals received).
    jsonObject["ru_nsignals"] = uvRusage.ru_nsignals;

    // Add ru_nvcsw (uint64_t, voluntary context switches).
    jsonObject["ru_nvcsw"] = uvRusage.ru_nvcsw;

    // Add ru_nivcsw (uint64_t, involuntary context switches).
    jsonObject["ru_nivcsw"] = uvRusage.ru_nivcsw;
}